<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
class Default_Model_KeywordMapper extends At_Model_Mapper{
    protected $tableName = 'keyword';

	/**
	 * Find keyword by or by id. does not assert the id, since this
	 * has had have been checked previously. watch
	 *
	 * @param Default_Model_Keyword $keyword keyword to look for ( name, id );
	 */
    public function find( Default_Model_Keyword $keyword ){
		$db 	= $this->getTable()->getAdapter();
		$value  = $db->fetchRow( 'SELECT * FROM keyword WHERE id = ?', $keyword->id );

		if( key_exists( 'name', $value ) ){
			$value = (object) $value;
			$keyword->id 	    = $value->id;
			$keyword->domainId  = $value->domain_id;
			$keyword->name      = $value->name;
		}

        return $keyword;
    }

	/**
	 * Find keyword by name. This function has been split from
	 * find by id to prevent recursion
	 * @todo findByName should look at domainId?
	 */
	public function findByName( Default_Model_Keyword $keyword ){
		$select = $this->getTable()->select();

		$select->where( 'name = ?', $keyword->name );
		$select->where( 'domain_id = ?', $keyword->domainId );

		$values = $this->getTable()->fetchRow( $select );

		if( $values ){
			foreach( $values->toArray() as $key => $value ){
				$keyword->$key = $value;
			}
		}

		return $keyword;
	}

	/**
	 * Save a keyword
	 *
	 * - update if id is set
	 * - insert else
	 *
	 * @param Default_Model_Keyword $keyword values to save
	 */
    public function save( Default_Model_Keyword &$keyword ){
        $values = array(
            'name'       => $keyword->name,
            'domain_id'  => $keyword->domainId
        );

		if( null == $keyword->id ){
			$keyword->id = $this->getTable()->insert( $values );
		}
		else{
            $this->getTable()->update($values, array('id = ?' => $keyword->id));
		}

        return $keyword;
    }

	/**
	 * Fetches all keywords. Takes a configuration from the provided $keyword object
	 * - if channel is set, fetch keywords for channel $channel
	 * - else if annotation is set, fetch keywords for annotation
	 * - else if domainid  is set, fetch keywords for domain_id only
	 *
	 * @param Default_Model_Keyword $keyword A keyword object
	 */
    public function fetchAll( Default_Model_Keyword $keyword ){
		$db = $this->getTable()->getAdapter();
		$bind = array();

		if( null !== $keyword->getChannel() && null !== $keyword->getChannel()->id ){
			$query = '
				SELECT k.*
				FROM channel_keyword ck
				LEFT JOIN keyword k ON k.id = ck.keyword_id
				WHERE ck.channel_id = :channel_id
			';

			$bind['channel_id'] = $keyword->getChannel()->id;
		}
		else if( null !== $keyword->getAnnotation() && null !== $keyword->getAnnotation()->id ){
			$query = '
				SELECT k.*
				FROM annotation_keyword ak
				LEFT JOIN keyword k ON k.id = ak.keyword_id
				WHERE ak.annotation_id = :annotation_id
			';

			$bind['annotation_id'] = $keyword->getAnnotation()->id;
		}
		else{
			$query = '
				SELECT k.*
				FROM keyword k
			';

			if( null !== $keyword->domainId && $keyword->domainId > 0 ){
				$query .= ' WHERE domain_id = :domain_id';
				$bind['domain_id'] = $keyword->domainId;
			}
		}

		$query .= ' ORDER BY k.domain_id, k.name';

		$result = $db->fetchAll( $query, $bind );

		$collect = array();

		foreach( $result as $row ){
			$collect[] = new Default_Model_Keyword( $row );
		}

		return $collect;

    }
}
